package cn.me.service.impl;

import cn.me.dao.PayMapper;
import cn.me.dao.PayrankMapper;
import cn.me.pojo.Pay;
import cn.me.pojo.PayExample;
import cn.me.pojo.Payrank;
import cn.me.pojo.toolPojo.ToCount;
import cn.me.service.PayService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Transactional
@Service
public class PayServiceImpl implements PayService {
    @Autowired
    private PayMapper payMapper;
    @Autowired
    private PayrankMapper payrankMapper;
    @Override
    public PageInfo<Pay> selectby(int id, String after, String before, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        PayExample example=new PayExample();
        PayExample.Criteria criteria = example.createCriteria();
        criteria.andPuidEqualTo(id);
        //判断数据合法性
        boolean isAfter = isdate(after);


        boolean isBefore = isdate(before);

        System.out.println(isAfter+"---"+isBefore);
        boolean isok=true;
        if (isAfter&&isBefore){
            try {
                System.out.println("进来1");
                isok=false;
                Date dateAfter = new SimpleDateFormat("yyyy-MM-dd").parse(after);
                Date dateBefore = new SimpleDateFormat("yyyy-MM-dd").parse(before);
                criteria.andPadteBetween(dateAfter,dateBefore);
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }

        if (isAfter&&isok){
            try {
                System.out.println("进来2");
                Date dateAfter = new SimpleDateFormat("yyyy-MM-dd").parse(after);
                criteria.andPadteGreaterThanOrEqualTo(dateAfter);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        if (isBefore&&isok){
            try {
                System.out.println("进来3");
                Date dateBefore = new SimpleDateFormat("yyyy-MM-dd").parse(before);
                System.out.println(dateBefore+"----------");
                criteria.andPadteLessThanOrEqualTo(dateBefore);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        List<Pay> pays = payMapper.selectByExample(example);
        for (Pay pay : pays) {
            pay.setPayrank(payrankMapper.selectByPrimaryKey(pay.getPrank()));
        }


        return  new PageInfo<>(pays);

    }

    @Override
    public void insert(Pay pay) {
        payMapper.insertSelective(pay);
    }

    private boolean isdate(String date){
        System.out.println("来判断了+"+date);
        return date.trim().length()==10&&String.valueOf(date.charAt(4)).equals("-")&&
                String.valueOf(date.charAt(7)).equals("-");

    }

    @Override
    public Pay toCount(ToCount count) {
        Pay pay = new Pay();
        /*工资是先扣除五险一金再扣除个税的，其中社保和公积金是同步扣除，
        不存在先后顺序，扣完五险一金之后，再减去个税专项扣除项目，
        专项扣除项目包括子女教育、继续教育、赡养父母、房贷/房租、大病医疗五大项。*/
        //1.先扣五险一金再扣个税
            double five=360.00d;//五险一金
        pay.setPfive(five);
            //五险一金统一:360
            double money1=count.getMoney()-five-count.getPment();
        //2.奖金不算个税
        double tax =Double.valueOf(tax(money1));//个税
        pay.setPtax(tax);
        //实发
        pay.setPpay(count.getMoney()+count.getPsubsidy()-five-tax-count.getPment());
        return pay;
    }

    private String tax(double d){
        DecimalFormat df= new DecimalFormat("#0.00");

        /*剩下的金额如果在5000元以上，就需要扣个人所得税，如果在5000元以下，
        就免除缴纳个税。
        应纳个人所得税税额=应纳税所得额×适用税率-速算扣除数
        */
        //不超过8k
        if (d>5000d&&d<=8000d){
            return df.format((d-5000d)*0.03d-0d);
        }
        //超过8k
        if (d>8000d&&d<=17000d){
            return df.format(((d-5000d)*0.1d-210d));
        }
        //超过17k
        if (d>17000d&&d<=30000d){
            return df.format((d-5000d)*0.2d-1410d);
        }
        //超过30k
        if (d>30000d){
            return df.format((d-5000d)*0.25d-2660d);
        }
        return df.format(0.00d);
    }

    @Override
    public void delectBypuid(Integer puid) {
        PayExample example=new PayExample();
        PayExample.Criteria criteria = example.createCriteria();
        criteria.andPuidEqualTo(puid);
        payMapper.deleteByExample(example);


    }
}
